package com.example.huawei.helloworld;

//----------------------------------------------------------------------
//  模拟栈.java    Author: 唐才铭   Date：18/05/28-20:12
//  
//  练习使用安卓开发程序来模拟栈的push、pop功能。
//----------------------------------------------------------------------

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Stack;

public class MainActivity extends AppCompatActivity {

    Stack<String> stack = new Stack();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast toast = Toast.makeText(this,"TEST", Toast.LENGTH_LONG);

        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new myButtonOnClickListener1());
        Button button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(new myButtonOnClickListener2());
    }

    public class myButtonOnClickListener1 implements View.OnClickListener{
        public void onClick(View view){
            EditText editText1 = (EditText)findViewById(R.id.editText1);
            stack.push(editText1.getText().toString());
            EditText editText2 = (EditText)findViewById(R.id.editText2);


            editText2.setMovementMethod(ScrollingMovementMethod.getInstance());
            editText2.setText(editText2.getText(),TextView.BufferType.EDITABLE);
            editText2.setText("Push" + stack.toString(), TextView.BufferType.EDITABLE );
            System.out.println(editText2.getText().toString());
        }
    }

    public class myButtonOnClickListener2 implements View.OnClickListener{
        public void onClick(View view){

            EditText editText1 = (EditText)findViewById(R.id.editText1);
            stack.pop();
            EditText editText2 = (EditText)findViewById(R.id.editText2);


            editText2.setMovementMethod(ScrollingMovementMethod.getInstance());
            editText2.setSelection(editText2.getText().length(),editText2.getText().length());
            editText2.setText("Pop" + stack.toString(),TextView.BufferType.EDITABLE);
            System.out.println(editText2.getText().toString());
        }
    }
}